//
//  DataBaseManager.m
//  NewNest
//
//  Created by hesiyao on 16/1/24.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "DataBaseManager.h"

@implementation DataBaseManager

+(DataBaseManager *)shareInstance
{
    static DataBaseManager *dbManager = nil;
    
    if (!dbManager) {
        dbManager = [[DataBaseManager alloc] init];
    }
    
    return dbManager;
}
/// 打开数据库
- (void)openDB
{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"contact.db"];
    
    int result = sqlite3_open(path.UTF8String, &dbPoint);
    NSLog(@"打开数据库 %d 路径 : %@", result, path);
    
}
/// 关闭数据库
- (void)closeDB
{
    sqlite3_close(dbPoint);
}

/// 创建表

-(void)createTable
{
    NSString *createSQL = @"CREATE TABLE IF NOT EXISTS contact (contactName text, password text, mailbox text, phone text, contactId integer PRIMARY KEY AUTOINCREMENT)";
    int result = sqlite3_exec(dbPoint, createSQL.UTF8String, NULL, NULL, NULL);
    [self judgeResult:result message:@"创建表"];
}

/// 增加联系人

//-(void)insertContact:(ContactModel *)contact
//{
//    NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO contact (contactName, password, mailbox, phone) values ('%@', '%@', '%@', '%@')", contact.contactName, contact.password, contact.mailbox, contact.phone];
//    int result = sqlite3_exec(dbPoint, insertSQL.UTF8String, NULL, NULL, NULL);
//    [self judgeResult:result message:@"添加联系人"];
//}

/// 查询

//-(NSMutableArray *)select
//{
//    NSString *selectSQL = @"SELECT * FROM contact";
//    sqlite3_stmt *stmt = nil;
//    int result = sqlite3_prepare(dbPoint, selectSQL.UTF8String, -1, &stmt, nil);
//    NSMutableArray *arr = [NSMutableArray array];
//    if (result == SQLITE_OK) {
//        NSLog(@"查询准备成功");
//        while (sqlite3_step(stmt) == SQLITE_ROW) {
//            const unsigned char *contactName = sqlite3_column_text(stmt, 0);
//            const unsigned char *password = sqlite3_column_text(stmt, 1);
//            const unsigned char *mailbox = sqlite3_column_text(stmt, 2);
//            const unsigned char *phone = sqlite3_column_text(stmt, 3);
//            int contactId = sqlite3_column_int(stmt, 4);
//            ContactModel *m = [ContactModel new];
//            m.contactName = [NSString stringWithUTF8String:(const char *) contactName];
//            m.password = [NSString stringWithUTF8String:(const char *) password];
//            m.mailbox = [NSString stringWithUTF8String:(const char *) mailbox];
//            m.phone = [NSString stringWithUTF8String:(const char *) phone];
//            m.contactId = contactId;
//            [arr addObject:m];
//        }
//    }else
//    {
//        NSLog(@"查询失败%d", result);
//    }
//    return arr;
//}

///判断方法
- (void)judgeResult:(int)result message:(NSString *)message
{
    if (result == SQLITE_OK) {
        NSLog(@"%@成功", message);
    }else{
        NSLog(@"%@失败, %d", message, result);
    }
}


@end
